Skip to content

feat(approval): answer a permission request from a Feishu card - #400

Open
tombang wants to merge 1 commit into
wxtsky:mainfrom
tombang:feat/feishu-remote-approval
Open

tombang wants to merge 1 commit into
wxtsky:mainfrom
tombang:feat/feishu-remote-approval

Conversation

@tombang

@tombang tombang commented Nov 8, 2026 •

Copy link
Copy Markdown

Summary

  • Add optional Feishu remote approval for hook PermissionRequests. A company self-built bot sends a private card with Allow once and Deny.
  • A text reply never approves a command. The decision is applied to that request id, not whichever card is currently on screen, and there is no remote Always allow.
  • The App Secret is stored in the Keychain. The one-way Feishu webhook under Push notifications is unchanged and still cannot approve anything.
  • The release build embeds feishu-bridge, the official Go SDK long connection. CI installs Go 1.23 before build-dmg.sh.

This was tested locally against a self-built Feishu app: deny, allow once, and a text reply that must not approve.

Test plan

  • Build an arm64 DMG and confirm Contents/Helpers/feishu-bridge is present.
  • In Settings → Behavior → Feishu remote approval, save an App ID and App Secret, connect, then bind the account that messages the bot.
  • Trigger a Codex command that needs approval. Deny it from Feishu and confirm the command does not run.
  • Trigger it again, tap Allow once, and confirm only that command runs.
  • Reply “同意” in the chat and confirm it does not approve.

A company self-built Feishu bot can receive a private card for a
PermissionRequest, with Allow once and Deny. A text reply never approves,
and the App Secret stays in the Keychain.
@tombang
tombang force-pushed the feat/feishu-remote-approval branch from 8c40291 to 4ba5559 Compare November 9, 2026 05:53
@wxtsky

wxtsky commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Thanks a lot for this, @tombang. It's a carefully thought-out feature: the decision goes to the request id rather than whatever card is on screen, there's no remote Always allow, chat text never counts as an answer, the App Secret goes over stdin instead of argv or the environment, and every way a request can leave the queue is reported back to the helper. Those are exactly the right instincts, and they made this much easier to review.

Why this stays open for now: this PR changes the release build. build-dmg.sh now needs a Go toolchain, and a second, Go-built executable goes inside the signed and notarized DMG. It also opens a new channel that can answer approvals from outside the Mac. I don't merge changes like that in a batch: I want to verify signing, notarization and a real Feishu tenant on my own machine first, and then decide.

Meanwhile, here's what the review found, most important first. We built it, ran the Swift and Go tests, and probed the helper offline. Nothing was run against a Feishu tenant.

Needs changes before it can merge

  1. Pairing can bind someone else's account. While "Start binding" is armed, every private message to the bot becomes a candidate. main.go keeps armBind set after the first one, and only bind/unbind clear it. FeishuApprovalCenter overwrites pairCandidateOpenID each time, the arm never expires, and Settings shows only an ou_… open_id that nobody can recognise. Suppose the app's availability scope includes anyone else and a colleague messages the bot between your message and your click on "Bind this account": the colleague gets bound, and their taps then approve your agent's commands. Please use a one-time code shown in Settings (say 6–8 digits, valid for about 5 minutes): bind only the sender whose message is exactly that code, and disarm on the first match. Also clear the binding when the App ID changes. Open_ids are per app, and save(appID:secretInput:) currently keeps the old one.

  2. A write to a dead helper kills CodeIsland. send() writes to the helper's stdin on a background queue (FeishuApprovalCenter.swift:336-344). CodeIsland does not ignore SIGPIPE, and try? can't catch a signal. So if the helper has crashed, been quit in Activity Monitor, or just been stopped while a card/resolved line is still queued, the whole island exits, and every held approval loses its answer. A small repro with the same write exits with status 141. With fcntl(fd, F_SETNOSIGPIPE, 1) on the pipe's write end, the write throws instead and the process carries on. Please set that right after creating the pipe, and treat a failed write as "helper gone".

  3. The pipe handlers spin when the helper exits (FeishuApprovalCenter.swift:204-215). At EOF availableData is empty, and the if lines.isEmpty && !data.isEmpty { return } guard lets that through, so every spin also schedules a Task { @MainActor … }. A stand-alone repro with the same handler shape and object lifetimes logged roughly 350k–490k handler calls and 150k–200k empty main-actor tasks in the first second after the child exited. That happened after a crash and after stop() alike, and lasted until the Process was released. On empty data, set readabilityHandler = nil and return, and only hop to the main actor when there are lines.

  4. L10n. The 15 feishu_approval_* keys exist only in en and zh, so three L10nTests fail (the Japanese, Korean and Turkish key checks), and zh-Hant, ja, ko, tr and de users see English. Every new key needs all seven tables. There's also a mixed-language problem. The Settings status line shows the helper's detail text as it is, and those strings, like the toasts, bot replies and card text, are hard-coded Simplified Chinese, while the helper's log lines are English. So an English user sees "长连接已就绪…" in Settings, and a Chinese user sees "card update failed". If the helper sends codes that the app maps to L10n keys, and the app passes the card labels in its own language, both problems go away. Self.text(zh, en) should go through L10n too; today it gives zh-Hant users Simplified Chinese.

  5. Release build. Without Go, build-dmg.sh now fails outright, and the toolchain isn't pinned. go 1.23 in go.mod and go-version: '1.23' in CI are past end of life (go.dev lists 1.27.x and 1.26.x as the supported releases), and a local build uses whatever Go happens to be installed. This is a network-facing binary we'd ship to everyone, so please pin it:

    • toolchain go1.27.x in go.mod;
    • go-version-file: helpers/feishu-bridge/go.mod and cache-dependency-path: helpers/feishu-bridge/go.sum in the workflow;
    • make the script fail on an older Go.

    Raising the go line also drops the 1.23-era GODEBUG defaults that go version -m shows today (rsa1024min=0, tlsmlkem=0, …). Also, ./build.sh, the README's build-from-source path, doesn't embed the helper, so those builds say "feishu-bridge is missing". One thing I'll weigh myself: the universal helper is about 15 MB (about 5.9 MB compressed). That would make the DMG (15.7 MB for v1.0.41) roughly a third bigger for every download and update.

Should fix

  1. Keychain at launch, on the main thread, and in tests. FeishuApprovalCenter.init reads the secret's data on every launch, even with the feature off, and again each time Settings appears. spawn() does the Keychain read and Process.run() on the main actor. AppState touches FeishuApprovalCenter.shared on every permission, so swift test reads the login keychain too. Once a secret is saved, xctest and every ad-hoc dev rebuild get the "wants to use your confidential information" prompt, which blocks the main actor. Please check that the secret exists without reading its data (an attributes-only query doesn't prompt), read the data off the main thread only when starting the helper, and inject the center into AppState so tests get none.

  2. Cards never show the outcome. Feishu's "update a sent card" API (PATCH im/v1/messages/:message_id) requires "update_multi": true in the config of both the old and the new card. cardDocument only sets wide_screen_mode, so markResolved can't update the card. After "Allow once" the card stays at "处理中 … 正在等 Mac 确认", and a request answered on the Mac keeps live buttons in Feishu.

  3. Cards that never arrive or stop working.

    • flush() marks a request as sent as soon as the line is written, and the helper registers the card before the API call. A send that fails (network, rate limit, token) is never retried, so that request never reaches Feishu.
    • After the helper restarts, its card map is empty and the app doesn't offer pending requests again. Tapping a card whose request is still pending then says "这条审批已经结束", while the command is still waiting on the Mac.
    • REST calls have no timeout: lark.NewClient without WithReqTimeout uses http.DefaultClient. And sendCard/markResolved run inline in the stdin loop, so one hung request stalls every later card and resolution.
    • reply runs inside the event handler, and Feishu pushes the event again if handling takes over 3 s.

    Please have the helper report back whether each card was sent, offer pending requests again after a restart, add per-request timeouts, and move the network calls to worker goroutines.

  4. "online" isn't a connection state. arm_bind, bind, unbind and configure always reply status: online. Fed to the helper with no connection at all, they gave online three times. So Settings can say connected while the connection is down, flush() goes ahead and sends, and the restart backoff resets. Please keep the connection state separate from binding feedback.

  5. Only run the bundled helper. helperURL() also accepts CODEISLAND_FEISHU_BRIDGE and a feishu-bridge next to the app, which is /Applications/feishu-bridge for an installed copy. Whatever runs gets the App Secret, and its stdout is taken as your decision. The open_id check in apply() compares with the id the app itself sent in configure, so it can't stop a substitute binary. In release builds, please launch only Contents/Helpers/feishu-bridge, ideally after checking it is signed by the same team, and keep the override for debug builds and tests.

  6. Say what leaves the Mac, and for which requests. Every agent's PermissionRequest is offered, not only Codex's as the Settings text says. It also always goes out, not only while you're away, which is how pushes work by default. The card carries the redacted command (good: it goes through PushMessageFormatter.clean), plus the project path, tool, source and session id, and these then stay in the tenant's chat history. Settings should say that plainly. The card also shows only the first matching field: for Edit/Write the path but not the change, for a plan only a one-line description. So "Allow once" can approve something the user never saw. Consider offering remote Allow only where the card shows the whole action (shell commands), with Deny or "review on the Mac" otherwise, plus an "only while I'm away" option.

  7. Tests. Only parseDecision is tested. Please add:

    • Swift tests for approvePermission(requestId:) and denyPermission(requestId:): an unknown id returns false and answers nothing, and a Codex "Allow once" writes no rule;
    • Swift tests for the card_action gate: a wrong or unpaired open_id, and malformed lines;
    • a Swift test that every way a request leaves the queue reports gone;
    • Go tests for onCard with constructed events: another operator, and an unknown, finished or in-flight id.

    None of these need the network.

Smaller things

  • Tie a click to the card it came from: check event.Event.Context.OpenMessageID against that card's message id. Anyone with the App Secret, for example collaborators on the app in the developer console, can send cards as the bot.
  • Feishu delivers each event to one random long connection of an app, so the same bot on two Macs loses clicks and pairing messages at random. Worth saying "one Mac per bot" in the docs.
  • FeishuSecretStore.save deletes before adding, so a failed add loses the old secret; SecItemUpdate avoids that. kSecAttrAccessible has no effect in the file-based keychain.
  • The App ID field writes to defaults on every keystroke (@AppStorage), so a helper restart can pick up a half-typed ID.
  • The restart backoff never gives up: it goes from 2 s to 30 s and then retries every 30 s forever.
  • Turning the feature off leaves live cards in Feishu; mark them ended first.
  • Lark (larksuite.com) tenants can't connect, because the SDK defaults to open.feishu.cn. Either say Feishu only or add WithDomain.
  • The diagnostics export should list whether the feature is on, whether an account is bound, and the connection state (not the App ID or open_id).

What checked out:

  • Signing: the existing inside-out loop over Contents/Helpers/* already signs the new binary with hardened runtime and a timestamp, and it needs no entitlements. An ad-hoc --options runtime copy verifies and runs, and its LC_BUILD_VERSION SDK 11.0 meets notarization's minimum.
  • The secret never shows in ps, and nothing is logged.
  • The helper exits when CodeIsland quits.
  • The idle helper is cheap: 0.0% CPU and about 10 MB without a connection. With one, the SDK pings every 2 minutes.

Developer ID signing and notarization still need my own run.

Thanks again. The core design is sound, and with these addressed it'll be much easier to say yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants